<template>
	<view>
		<view class="nav-bar">
			<NavBar>
				<navigator open-type="navigateBack" hover-class="none" class="nav-back-view" slot="left">
					<image src="/static/nav-back-icon.png" class="nav-back"></image>
				</navigator>

				<view slot="center" class="nav-title">消息通知</view>
				<view @click="onAllEvent" class="nav-right-view" slot="right">
					<image src="/static/message-all.png" class="nav-add"></image>
					<view>全部已读</view>
				</view>
			</NavBar>
		</view>
		<view v-for="(item, index) in dataList" :key="index" @click="onDetailEvent(item)" class="card-view">
			<image v-if="item.readFlag == '10'" src="/static/message-icon.png" class="card-image"></image>
			<view class="name-view">{{item.title}}</view>
			<view class="desc-text">{{item.content}}</view>
			<view class="time-view">{{dateFormat(item.createTimes, 'YYYY年MM月DD日 HH:mm')}}</view>
		</view>
		<view style="height: 160upx;"></view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				dataList: [],
				queryData: {
					total: 0,
					pageNo: 1,
					pageSize: 15,
				}
			}
		},
		onLoad(options) {
			this.getRequestList()
		},
		onPullDownRefresh() {
			this.queryData.pageNo = 1
			this.getRequestList()
		},
		onReachBottom() {
			if (this.queryData.total <= this.dataList.length) return
			this.queryData.pageNo += 1
			this.getRequestList()
		},
		methods: {
			async onAllEvent() {
				uni.$util.showLoading()
				await uni.$api.allRead().finally(uni.finally)
				this.queryData.pageNo = 1
				this.getRequestList()
			},
			async getRequestList() {
				uni.$util.showLoading()
				const res = await uni.$api.getMessagePage(this.queryData).finally(uni.finally)
				const list = res.data.rows || []
				this.queryData.total = res.data.totalRows
				this.dataList = this.queryData.pageNo == 1 ? list : this.dataList.concat(list)
			},
			onDetailEvent(item) {
				uni.navigateTo({
					url: `/pages/messageDetail/messageDetail?messageId=${item.messageId}`,
					success: () => item.readFlag = '20'
				})
			},
		}
	}
</script>

<style scoped lang="scss">
	.nav-bar {
		position: sticky;
		top: 0;
		left: 0;
		z-index: 20;

		.nav-back-view {
			@extend .column;

			.nav-back {
				width: 43upx;
				height: 43upx;
			}
		}

		.nav-title {
			font-size: 33rpx;
			font-family: PingFangSC, PingFang SC;
			font-weight: 400;
			color: #FFFFFF;
		}

		.nav-right-view {
			@extend .column;
			font-size: 22rpx;
			font-family: PingFangSC, PingFang SC;
			font-weight: 400;
			color: #FFFFFF;

			.nav-add {
				width: 31upx;
				height: 31upx;
			}
		}

	}

	.card-view {
		width: 707rpx;
		background: #FFFFFF;
		border-radius: 14rpx;
		padding: 18upx 29upx;
		padding-bottom: 0;
		margin: 0 auto;
		margin-top: 18upx;
		position: relative;

		.card-image {
			position: absolute;
			width: 74upx;
			height: 38upx;
			right: 16upx;
			top: -5upx;
		}

		.name-view {
			width: 100%;
			@extend .row-start;
			font-size: 29rpx;
			font-weight: 600;
			color: #2C1101;

		}

		.desc-text {
			width: 100%;
			font-size: 25rpx;
			font-weight: 600;
			color: #B8ACA3;
			margin-top: 10upx;
		}

		.time-view {
			width: 100%;
			@extend .row-end;
			padding: 20upx 0;
			border-top: 1px solid #F7E8E4;
			margin-top: 30upx;
			font-size: 25rpx;
			font-family: PingFangSC, PingFang SC;
			font-weight: 400;
			color: #B8ACA3;
		}
	}
</style>